﻿using System;
using System.Web;
using System.Web.UI.WebControls;
using Core.Authenticated;
using Core.Library;
using DataObject.Customer;
using DataObject.Product;

namespace CMS.Controls.Customer
{
    public partial class CustomerManager : AuthenticatedControl
    {
        protected int PageSize;
        protected int PageIndex;
        protected int TypeId;
        protected string Keyword;

        protected void Page_Load(object sender, EventArgs e)
        {
            PageIndex   = Utils.GetIdFromRequestString(Utils.PageIndex, SettingValues.PageIndex);
            PageSize    = Utils.GetIdFromRequestString(Utils.PageSize, ConvertUtility.ToInt32(ddlPageSize.SelectedValue));
            TypeId      = Utils.GetIdFromRequestString(Utils.TypeId, 0);
            Keyword     = Utils.GetIdFromRequestString(Utils.Keyword, string.Empty);

            if (!IsPostBack)
                ListData();
        }

        private void ListData()
        {
            var totalRecords = 0;
            var customers = CustomerController.GetListSearchCustomer(TypeId, Keyword, PageIndex, PageSize);

            lvList.DataSource = customers;
            lvList.DataBind();

            ltPager.Text = PagerUtility.PageLinks(PageIndex, PageSize, totalRecords, 5,
                                                    UrlHelpers.PaginationLinkCustomer(TypeId, Keyword, "{0}", ddlPageSize.SelectedValue));

            if (TypeId > 0)
                ddlType.SelectedValue = TypeId.ToString();

            if (!string.IsNullOrEmpty(Keyword))
                txtSearch.Text = Keyword;
        }

        protected void lvList_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            var dataItem = (ListViewDataItem)e.Item;
            var id = ConvertUtility.ToInt32(lvList.DataKeys[dataItem.DisplayIndex].Value);

            if (id > 0)
            {
                var o = CustomerController.Customer_GetById(id);
                if (o != null)
                {
                    if (string.Equals(e.CommandName, "DeleteCustomer"))
                    {
                        try
                        {
                            CustomerController.Customer_Delete(id);
                            ltMessage.Text = MessageUtility.ShowMessage(string.Empty, "Xóa dữ liệu thành công", MessageType.Success);
                            ListData();
                        }
                        catch
                        {
                            ltMessage.Text = MessageUtility.ShowMessage(string.Empty, "Bạn cần xóa các thông tin liên quan đến hóa đơn. Trước khi xóa khách hàng này.", MessageType.Information);
                        }
                    }
                    else if (string.Equals(e.CommandName, "ActiveCustomer"))
                    {
                        CustomerController.Customer_Update_Status(id, true);
                        ltMessage.Text = MessageUtility.ShowMessage(string.Empty, "Kích hoạt " + o.Username + " thành viên thành công", MessageType.Success);
                        ListData();
                    }
                    else if (string.Equals(e.CommandName, "NotActiveCustomer"))
                    {
                        CustomerController.Customer_Update_Status(id, false);
                        ltMessage.Text = MessageUtility.ShowMessage(string.Empty, "Tạm dừng " + o.Username + " thành viên thành công", MessageType.Success);
                        ListData();
                    }
                }
            }
        }

        protected void BtnSearch(object sender, EventArgs e)
        {
            RedirectLink();
        }

        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            RedirectLink();
        }

        private void RedirectLink()
        {
            var keyword  = HttpUtility.UrlEncode(txtSearch.Text);
            var typeId   = ConvertUtility.ToInt32(ddlType.SelectedValue);
            var pageSize = ddlPageSize.SelectedValue;

            Response.Redirect(UrlHelpers.PaginationLinkCustomer(typeId, keyword, SettingValues.PageIndex.ToString(), pageSize));
        }

        protected void lvList_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                var isActive    = ConvertUtility.ToBoolean(((HiddenField)e.Item.FindControl("hfIsActive")).Value);
                var lbActive    = (LinkButton)e.Item.FindControl("lbActive");
                var lbNotActive = (LinkButton)e.Item.FindControl("lbNotActive");

                if (isActive)
                {
                    lbActive.Visible    = false;
                    lbNotActive.Visible = true;
                }
                else
                {
                    lbActive.Visible    = true;
                    lbNotActive.Visible = false;
                }                
            }
        }        
    }
}